﻿using FlashPay.DAO.Bank;
using FlashPay.DAO.Interface;
using FlashPay.DAO.Order;
using FlashPay.EF.Models;
using FlashPay.Entity;
using FlashPay.Entity.Enum;
using FlashPay.Entity.Parameter;
using FlashPay.Entity.Request;
using FlashPay.Entity.Request.Bank;
using FlashPay.Entity.Response.Bank;
using FlashPay.Entity.Response.BankCard;
using FlashPay.Entity.Response.BankCardExtraLimit;
using FlashPay.Entity.Response.Company;
using FlashPay.Service.Interface;
using FlashPay.Util;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;

namespace FlashPay.Service.Impl
{
    public class BankCardServicelmpl:BankCardService
    {
        #region 注入
        /// <summary>
        /// 银行卡数据接口
        /// </summary>
        private readonly BankCardDao _bankCardDao;

        /// <summary>
        /// 公司数据接口
        /// </summary>
        private readonly CompanyDao _companyDAO;

        /// <summary>
        /// 银行数据接口
        /// </summary>
        private readonly BankInfoDAO _bankInfoDAO;

        /// <summary>
        /// 用户数据接口
        /// </summary>
        private readonly UserInfoDao _userInfoDao;

        /// <summary>
        /// 菜单数据接口
        /// </summary>
        private readonly MenuDao _menuDao;

        /// <summary>
        /// 日志权限数据接口
        /// </summary>
        private readonly LogDao _logDao;

        /// <summary>
        /// 订单数据接口
        /// </summary>
        private readonly OrderRecordDAO _orderRecordDao;

        /// <summary>
        /// 日志模型
        /// </summary>
        private readonly LogRecord _logRecord;

        public BankCardServicelmpl(BankCardDao bankCardDao, CompanyDao companyDAO, BankInfoDAO bankInfoDAO, UserInfoDao userInfoDao, LogDao logDao, LogRecord logRecord,MenuDao menuDao, OrderRecordDAO orderRecordDao)
        {
            this._bankCardDao = bankCardDao;
            this._companyDAO = companyDAO;
            this._bankInfoDAO = bankInfoDAO;
            this._userInfoDao = userInfoDao;
            this._logDao = logDao;
            this._logRecord = logRecord;
            this._menuDao = menuDao;
            this._orderRecordDao = orderRecordDao;
        }
        #endregion

        /// <summary>
        /// 根据编号获取银行卡信息
        /// </summary>
        public BankCardResponse GetById(int id)
        {
            var bankCardResponse = new BankCardResponse();

            var bankCard = _bankCardDao.GetBankCardByBcid(id);
            if (bankCard != null) {
                bankCardResponse.Bcid = bankCard.Bcid;
                bankCardResponse.CardType = bankCard.CardType;
                bankCardResponse.CompanyId = bankCard.CompanyId;
                bankCardResponse.OrderNo = bankCard.OrderNo.ToString();
                bankCardResponse.BankCode = bankCard.BankCode;
                bankCardResponse.CardNumber = bankCard.CardNumber;
                bankCardResponse.SecCardNumber = bankCard.SecCardNumber;
                bankCardResponse.CardName = bankCard.CardName;
                bankCardResponse.UsingStatus = bankCard.UsingStatus;
                bankCardResponse.EnableStatus = bankCard.EnableStatus;
                bankCardResponse.LoginName = bankCard.LoginName;
                bankCardResponse.PasswordLogin = EncryptHelper.deCryptDES(bankCard.PasswordLogin);
                bankCardResponse.PasswordQuery = EncryptHelper.deCryptDES(bankCard.PasswordQuery);
                bankCardResponse.PasswordPay = EncryptHelper.deCryptDES(bankCard.PasswordPay);
                bankCardResponse.PasswordShield = EncryptHelper.deCryptDES(bankCard.PasswordShield);
                bankCardResponse.OriginalPassword = EncryptHelper.deCryptDES(bankCard.OriginalPassword);
                bankCardResponse.UsbType = bankCard.UsbType;
                bankCardResponse.UsbSerialNumber = bankCard.UsbSerialNumber;
                bankCardResponse.AccountBank = bankCard.AccountBank;
                bankCardResponse.DocumentNumber = bankCard.DocumentNumber;
                bankCardResponse.PhoneNumber = bankCard.PhoneNumber;
                bankCardResponse.PaymentStart = bankCard.PaymentStart;
                bankCardResponse.PaymentEnd = bankCard.PaymentEnd;
                bankCardResponse.PayFeeRatio = bankCard.PayFeeRatio;
                bankCardResponse.CrossBankPay = bankCard.CrossBankPay;
                bankCardResponse.DepositType = bankCard.DepositType;
                bankCardResponse.DepositFeeRatio = bankCard.DepositFeeRatio;
                bankCardResponse.Remark = bankCard.Remark;
                var order = _orderRecordDao.Get(bankCard.OrderNo);
                if (order != null)
                {
                    bankCardResponse.DepositUID = order.DepositUid;
                    bankCardResponse.ReceiptUID = order.ReceiptUid;
                }
            }
            return bankCardResponse;
        }

        /// <summary>
        /// 根据银行卡号获取银行卡是否存在
        /// </summary>
        /// <param name="cardnumber">银行卡号</param>
        /// <returns>CardMerchant</returns>
        public bool GetBankCardByCardnumber(string Cardnumber)
        {
            return _bankCardDao.GetBankCardByCardnumber(Cardnumber);
        }
        /// <summary>
        /// 根据银行卡号获取银行卡额外限制是否存在
        /// </summary>
        /// <param name="cardnumber">银行卡号</param>
        /// <returns>CardMerchant</returns>
        public bool GetBankCardLimitByCardnumber(string Cardnumber)
        {
            return _bankCardDao.GetBankCardLimitByCardnumber (Cardnumber);
        }

        /// <summary>
        /// 获取所有銀行卡记录
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<BankCard> GetList(BankCardQuery query)
        {
            return _bankCardDao.GetList(query);
        }

        /// <summary>
        /// 获取库存卡报表
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>List<CardMerchant></returns>
        public List<BankCardExpResponse> GetPareListExp(BankCardQuery query)
        {
            #region 获取当前公司及子公司编号
            //参数赋值
            var companyRequest = new EF.Models.Company()
            {
                CompanyId = query.CompanyId.Value,
                CompanyPid = -1,
                CompanyStatus = 1
            };

            var companyQueryResult = new BaseModel<List<EF.Models.Company>>();
            //查询
            _companyDAO.Get(companyQueryResult, companyRequest, 2);

            if (companyQueryResult != null && companyQueryResult.Success)
            {
                var companyIds = new List<int>();

                if (companyQueryResult.Result != null && companyQueryResult.Result.Count > 0)
                {
                    companyQueryResult.Result.ForEach(item => {
                        companyIds.Add(item.CompanyId);
                    });
                }

                if (companyIds != null && companyIds.Any())
                {
                    query.CompanyIds = companyIds;
                }
            }
            #endregion

            var pager = _bankCardDao.GetPareListExp(query);

            return pager;
        }

        #region 分頁查詢所有銀行卡
        /// <summary>
        /// 分頁查詢所有銀行卡
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public PagedList<BankCardResponse> GetPager(BankCardQuery query,bool isSpare=false)
        {
            var pager = new PagedList<BankCardResponse>() {
                Success = false
            };
            try
            {
                #region 获取当前公司及子公司编号
                //参数赋值
                var companyRequest = new EF.Models.Company()
                {
                    CompanyId = query.CompanyId.Value,
                    CompanyPid = -1,
                    CompanyStatus = 1
                };

                var companyQueryResult = new BaseModel<List<EF.Models.Company>>();
                //查询
                _companyDAO.Get(companyQueryResult, companyRequest, 2);
                if (companyQueryResult != null && companyQueryResult.Success)
                {
                    var companyIds = new List<int>();

                    if (companyQueryResult.Result != null && companyQueryResult.Result.Count > 0)
                    {
                        if (!string.IsNullOrEmpty(query.CompanyName))
                        {
                            var companys = companyQueryResult.Result.Where(p => p.CompanyName.Contains(query.CompanyName)).ToList();
                            companys.ForEach(item => {
                                companyIds.Add(item.CompanyId);
                            });
                        }
                        else
                        {
                            companyQueryResult.Result.ForEach(item => {
                                companyIds.Add(item.CompanyId);
                            });
                        }
                    }


                    if (companyIds != null && companyIds.Any())
                    {
                        query.CompanyIds = companyIds;
                    }
                }
                #endregion

                var companylist = _companyDAO.Get();

                var bankcardList= _bankCardDao.GetPager(query, isSpare);
                if(bankcardList!=null)
                {
                    var bankcardResponseList = new List<BankCardResponse>();//输出BankCard模型
                    if(bankcardList.TData!=null)
                    {
                        foreach (var item in bankcardList.TData)
                        {
                            var bankcardResponse = new BankCardResponse()
                            {
                                Bcid = item.Bcid,
                                OrderNo = item.OrderNo.ToString(),
                                BankCode = item.BankCode,
                                CompanyId = item.CompanyId,
                                CompanyNameId=item.CompanyNameId,
                                AccountBank = item.AccountBank,
                                BankName = item.BankName,
                                CardName = item.CardName,
                                CardNumber = item.CardNumber,
                                SecCardNumber=item.SecCardNumber,
                                CardType = item.CardType,
                                CreateDate = item.CreateDate,
                                CreateUid = item.CreateUid,
                                CrossBankPay = item.CrossBankPay,
                                DepositFeeRatio = item.DepositFeeRatio,
                                DepositType = item.DepositType,
                                DocumentNumber = item.DocumentNumber,
                                EnableStatus = item.EnableStatus,
                                LoginName = item.LoginName,
                                OriginalPassword = item.OriginalPassword,
                                PasswordLogin = item.PasswordLogin,
                                PasswordPay = item.PasswordPay,
                                PasswordQuery = item.PasswordQuery,
                                PasswordShield = item.PasswordShield,
                                PayFeeRatio = item.PayFeeRatio,
                                PaymentEnd = item.PaymentEnd,
                                PaymentStart = item.PaymentStart,
                                PhoneNumber = item.PhoneNumber,
                                Remark = item.Remark,
                                UsbType = item.UsbType,
                                UsingStatus = item.UsingStatus
                            };
                            var company = companylist.Result.FirstOrDefault(p => p.CompanyId == item.CompanyNameId);
                            var userlist = _userInfoDao.Get(item.CreateUid);
                            if (company != null)
                            {
                                bankcardResponse.CompanyResponse = new CompanyResponse()
                                {
                                    CompanyID = company.CompanyId,
                                    CompanyName = company.CompanyName,
                                    CompanyNameEN = company.CompanyNameEn
                                };

                            }
                            if(userlist!=null)
                            {
                                bankcardResponse.UserInfoResponse = new Entity.Response.User.UserInfoResponse()
                                {
                                    UId = userlist.UId,
                                    ULoginName= userlist.ULoginName
                                };
                            }
                            bankcardResponseList.Add(bankcardResponse);
                        }
                        pager.TData = bankcardResponseList;
                    }
                    pager.CurrentPageIndex = bankcardList.CurrentPageIndex;
                    pager.TotalCount = bankcardList.TotalCount;
                    pager.Success = true;
                }
            }
            catch (Exception ex)
            {
                pager.ErrorMessage = ex.Message;
            }
            return pager;
        }
        #endregion

        #region 库存卡分页查询
        /// <summary>
        /// 库存卡查询
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public PagedList<SpareBankCardResponse> GetSparePager(BankCardQuery query) {

            #region 获取当前公司及子公司编号
            //参数赋值
            var companyRequest = new EF.Models.Company()
            {
                CompanyId = query.CompanyId.Value,
                CompanyPid = -1,
                CompanyStatus = 1
            };

            var companyQueryResult = new BaseModel<List<EF.Models.Company>>();
            //查询
            _companyDAO.Get(companyQueryResult, companyRequest, 2);

            if (companyQueryResult != null && companyQueryResult.Success)
            {
                var companyIds = new List<int>();

                if (companyQueryResult.Result != null && companyQueryResult.Result.Count > 0)
                {
                    companyQueryResult.Result.ForEach(item => {
                        companyIds.Add(item.CompanyId);
                    });
                }

                if (companyIds != null && companyIds.Any())
                {
                    query.CompanyIds = companyIds;
                }
            }
            #endregion

            var pager = _bankCardDao.GetSparePager(query);

            return pager;
        }
        #endregion

        #region 库存卡[添加、修改]
        /// <summary>
        /// 添加、修改银行卡
        /// </summary>
        /// <param name="request"></param>
        public JResult BankCardAddOrEdit(StockCardAddOrEditRequest request) {

            var result = new JResult()
            {
                Success = false
            };

            try
            {
                if (request.BCId > 0)
                {
                    //获取银行信息
                    var bankInfo = _menuDao.GetBankInfoByBankCode(request.BankCode);
                    if (bankInfo == null)
                    {
                        throw new Exception("银行信息不存在！");
                    }
                    //获取银行卡
                    var bankCard = _bankCardDao.GetBankCardByBcid(request.BCId);
                    if (bankCard == null)
                    {
                        throw new Exception("银行卡不存在！");
                    }

                    var orderRecord = GetById(request.BCId);
                    if (orderRecord == null)
                    {
                        throw new Exception("订单不存在！");
                    }
                    if (orderRecord.DepositUID ==0 || orderRecord.ReceiptUID ==0)
                    {
                        bankCard.OrderNo = long.Parse(request.OrderNo);
                    }
                    else {
                        bankCard.OrderNo = bankCard.OrderNo;
                    }

                    bankCard.BankCode = request.BankCode;
                    bankCard.BankName = bankInfo.BankName;
                    bankCard.CardType = request.CardType;
                    if (!string.IsNullOrEmpty(request.SecCardNumber))
                    {
                        bankCard.SecCardNumber = request.SecCardNumber;
                    }
                    bankCard.CardName = request.CardName;

                    bankCard.UsingStatus = request.UsingStatus;
                    bankCard.EnableStatus = request.EnableStatus;
                    bankCard.LoginName = request.LoginName;

                    bankCard.PasswordLogin = string.IsNullOrEmpty(request.PasswordLogin)? bankCard.PasswordLogin : EncryptHelper.enCryptDES(request.PasswordLogin);
                    bankCard.PasswordQuery = string.IsNullOrEmpty(request.PasswordQuery) ? bankCard.PasswordQuery : EncryptHelper.enCryptDES(request.PasswordQuery);
                    bankCard.PasswordPay = string.IsNullOrEmpty(request.PasswordPay) ? bankCard.PasswordPay : EncryptHelper.enCryptDES(request.PasswordPay);
                    bankCard.PasswordShield = string.IsNullOrEmpty(request.PasswordShield) ? bankCard.PasswordShield : EncryptHelper.enCryptDES(request.PasswordShield);

                    bankCard.UsbType = request.UsbType;
                    bankCard.UsbSerialNumber = request.UsbSerialNumber;
                    bankCard.OriginalPassword = string.IsNullOrEmpty(request.OriginalPassword) ? bankCard.OriginalPassword : EncryptHelper.enCryptDES(request.OriginalPassword);
                    bankCard.AccountBank = request.AccountBank;
                    bankCard.DocumentNumber = request.DocumentNumber;
                    bankCard.PhoneNumber = request.PhoneNumber;
                    bankCard.BankPhoto = request.BankPhoto;

                    #region 银行卡管理(收款卡、中转卡、付款卡)
                    if (bankCard.CardType != CardType.备用卡.GetHashCode())
                    {
                        bankCard.PaymentStart = request.PaymentStart;
                        bankCard.PaymentEnd = request.PaymentEnd;
                        bankCard.PayFeeRatio = request.PayFeeRatio;
                        bankCard.DepositFeeRatio = request.DepositFeeRatio;
                        bankCard.CrossBankPay = request.CrossBankPay;
                        bankCard.DepositType = request.DepositType;
                    }
                    if (request.CompanyId > 0)
                    {
                        bankCard.CompanyId = request.CompanyId;
                    }
                    #endregion

                    bankCard.Remark = string.IsNullOrEmpty(request.Remark) ? "" : request.Remark;

                    var status = _bankCardDao.Update(bankCard);
                    if (status)
                    {
                        result.Success = true;
                        result.SuccessMessage = "编辑卡成功！";
                    }
                    else {
                        result.Success = false;
                        result.SuccessMessage = "编辑卡失败！";
                    }
                }
                else {
                    //获取银行信息
                    var bankInfo = _menuDao.GetBankInfoByBankCode(request.BankCode);
                    if (bankInfo == null)
                    {
                        throw new Exception("银行信息不存在！");
                    }
                    //订单号验证
                    var orderRecord = _menuDao.GetOrderRecordByOrderNo(long.Parse(request.OrderNo));
                    if (orderRecord == null)
                    {
                        throw new Exception("订单号不存在！");
                    }

                    var bankCard = new BankCard();
                    bankCard.OrderNo = long.Parse(request.OrderNo);
                    bankCard.BankCode = request.BankCode;
                    bankCard.BankName = bankInfo.BankName;
                    bankCard.CardType = request.CardType;
                    bankCard.CardNumber = request.CardNumber;
                    if (!string.IsNullOrEmpty(request.SecCardNumber)) {
                        bankCard.SecCardNumber = request.SecCardNumber;
                    }
                    bankCard.CardName = request.CardName;
                    bankCard.UsingStatus = request.UsingStatus;
                    bankCard.EnableStatus = request.EnableStatus;

                    bankCard.LoginName = request.LoginName;

                    bankCard.PasswordLogin = string.IsNullOrEmpty(request.PasswordLogin) ? "" : EncryptHelper.enCryptDES(request.PasswordLogin);
                    bankCard.PasswordQuery = string.IsNullOrEmpty(request.PasswordQuery) ? "" : EncryptHelper.enCryptDES(request.PasswordQuery);
                    bankCard.PasswordPay = string.IsNullOrEmpty(request.PasswordPay) ? "" : EncryptHelper.enCryptDES(request.PasswordPay);
                    bankCard.PasswordShield = string.IsNullOrEmpty(request.PasswordShield) ? "" : EncryptHelper.enCryptDES(request.PasswordShield);

                    bankCard.UsbType = request.UsbType;
                    bankCard.UsbSerialNumber = request.UsbSerialNumber;
                    bankCard.OriginalPassword = string.IsNullOrEmpty(request.OriginalPassword) ? "" : EncryptHelper.enCryptDES(request.OriginalPassword);
                    bankCard.AccountBank = request.AccountBank;
                    bankCard.DocumentNumber = request.DocumentNumber;
                    bankCard.PhoneNumber = request.PhoneNumber;
                    bankCard.BankPhoto = request.BankPhoto;

                    #region 银行卡管理(收款卡、中转卡、付款卡)
                    if (bankCard.CardType != CardType.备用卡.GetHashCode())
                    {
                        bankCard.PaymentStart = request.PaymentStart;
                        bankCard.PaymentEnd = request.PaymentEnd;
                        bankCard.PayFeeRatio = request.PayFeeRatio;
                        bankCard.DepositFeeRatio = request.DepositFeeRatio;
                        bankCard.CrossBankPay = request.CrossBankPay;
                        bankCard.DepositType = request.DepositType;
                    }
                    if (request.CompanyId > 0) {
                        bankCard.CompanyId = request.CompanyId;
                    }
                    #endregion

                    bankCard.Remark = request.Remark;
                    bankCard.CreateUid = request.CreateUid;
                    bankCard.CreateDate = DateTime.Now;

                    var bankCardId = _bankCardDao.Insert(bankCard);
                    if (bankCardId>0)
                    {
                        result.Success = true;
                        result.SuccessMessage = "新增卡成功！";
                    }
                    else
                    {
                        result.Success = false;
                        result.SuccessMessage = "新增卡失败！";
                    }
                }
            }
            catch (Exception ex) {
                result.ErrorMessage = ex.Message;
            }

            return result;
        }
        #endregion

        #region 根据编号获取银行卡信息
        /// <summary>
        /// 根据编号获取银行卡信息
        /// </summary>
        /// <param name="bcid">银行卡编号</param>
        /// <returns>CardMerchant</returns>
        public JResult<BankCardResponse> GetBankCardByBcid(int bcid,int companyId,int createUid)
        {
            using (_bankCardDao)
            {
                var result = new JResult<BankCardResponse>();

                var bankCardResponse = new BankCardResponse();

                #region 银行卡信息
                var bankCard = _bankCardDao.GetBankCardByBcid(bcid);
                if (bankCard != null) {
                    bankCardResponse.Bcid = bankCard.Bcid;
                    bankCardResponse.CardType = bankCard.CardType;
                    bankCardResponse.CompanyId = bankCard.CompanyId;
                    bankCardResponse.OrderNo = bankCard.OrderNo.ToString();
                    bankCardResponse.BankCode = bankCard.BankCode;
                    bankCardResponse.CardNumber = bankCard.CardNumber;
                    bankCardResponse.SecCardNumber = bankCard.SecCardNumber;
                    bankCardResponse.CardName = bankCard.CardName;
                    bankCardResponse.UsingStatus = bankCard.UsingStatus;
                    bankCardResponse.EnableStatus = bankCard.EnableStatus;
                    bankCardResponse.LoginName = bankCard.LoginName;
                    bankCardResponse.PasswordLogin = EncryptHelper.deCryptDES(bankCard.PasswordLogin);
                    bankCardResponse.PasswordQuery = EncryptHelper.deCryptDES(bankCard.PasswordQuery);
                    bankCardResponse.PasswordPay = EncryptHelper.deCryptDES(bankCard.PasswordPay);
                    bankCardResponse.PasswordShield = EncryptHelper.deCryptDES(bankCard.PasswordShield);
                    bankCardResponse.OriginalPassword = EncryptHelper.deCryptDES(bankCard.OriginalPassword);
                    bankCardResponse.UsbType = bankCard.UsbType;
                    bankCardResponse.UsbSerialNumber = bankCard.UsbSerialNumber;
                    bankCardResponse.AccountBank = bankCard.AccountBank;
                    bankCardResponse.DocumentNumber = bankCard.DocumentNumber;
                    bankCardResponse.PhoneNumber = bankCard.PhoneNumber;
                    bankCardResponse.PaymentStart = bankCard.PaymentStart;
                    bankCardResponse.PaymentEnd = bankCard.PaymentEnd;
                    bankCardResponse.PayFeeRatio = bankCard.PayFeeRatio;
                    bankCardResponse.CrossBankPay = bankCard.CrossBankPay;
                    bankCardResponse.DepositType = bankCard.DepositType;
                    bankCardResponse.DepositFeeRatio = bankCard.DepositFeeRatio;
                    bankCardResponse.Remark = bankCard.Remark;

                    #region 图片
                    if (!string.IsNullOrEmpty(bankCard.BankPhoto)) {
                        bankCardResponse.DocumentResponse = JsonConvert.DeserializeObject<List<DocumentResponse>>(bankCard.BankPhoto);
                    }
                    #endregion

                    var order = _orderRecordDao.Get(bankCard.OrderNo);
                    if (order != null) {
                        bankCardResponse.DepositUID = order.DepositUid;
                        bankCardResponse.ReceiptUID = order.ReceiptUid;
                    }

                }
                #endregion

                #region 公司列表

                var companyIds = new List<int>();

                var companyRequest = new EF.Models.Company()
                {
                    CompanyId = companyId,
                    CompanyPid = -1,
                    CompanyStatus = 1
                };
                var companyList = new BaseModel<List<EF.Models.Company>>();

                _companyDAO.Get(companyList, companyRequest, 2);

                var companyResponse = new List<CompanyResponse>();

                if (companyList.Success)
                {
                    foreach (var item in companyList.Result)
                    {
                        companyIds.Add(item.CompanyId);

                        companyResponse.Add(new CompanyResponse()
                        {
                            CompanyID = item.CompanyId,
                            CompanyName = item.CompanyName,
                        });
                    }
                }
                bankCardResponse.CompanyResponseList = companyResponse;
                
                #endregion

                #region 银行
                var bankinfoList = _bankInfoDAO.GetBankInfoList();

                var bankinfoResponse = new List<BankInfoResponse>();

                if (bankinfoList != null)
                {
                    foreach (var item in bankinfoList)
                    {
                        bankinfoResponse.Add(new BankInfoResponse()
                        {
                            BankCode = item.BankCode,
                            BankName = item.BankName
                        });
                    }
                }
                bankCardResponse.BankInfoResponseList = bankinfoResponse;
                #endregion

                #region 订单列表
                var bankCards = _bankCardDao.GetAuditingOrderRecord(new UserInfoQuery() { CompanyIds = companyIds,Id= createUid });
                if (bankCards != null && bankCards.Any())
                {
                    bankCardResponse.AuditingOrderRecordResponseList = bankCards;
                }
                #endregion

                result.Success = true;
                result.Data = bankCardResponse;
                return result;
            }
        }
        #endregion

        /// <summary>
        /// 根据卡号或者用户名获取银行卡信息
        /// </summary>
        /// <param name="NumberOrName">银行卡号或者银行卡用户名</param>
        /// <param name="Strwhere">栏位</param>
        /// <returns></returns>
        public JResult<List<BankCard>> GetBankCardByNumberOrName(string NumberOrName, string Strwhere)
        {
            using (_bankCardDao)
            {
                JResult<List<BankCard>> result = new JResult<List<BankCard>>();
                var resultDAO = _bankCardDao.GetBankCardByNumberOrName(NumberOrName, Strwhere);
                if(resultDAO!=null)
                {
                    result.Success = true;
                    result.Data = resultDAO;
                }
                return result;
            }
        }

        /// <summary>
        /// 新增銀行卡信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public JResult AddBankCard(BankCard model, Request request, bool isSpare = false)
        {
            var result = new JResult()
            {
                Success = false
            };
            try
            {
                switch (model.CardType)
                {
                    case 1://收款卡
                        model.PaymentStart = 0;
                        model.PaymentEnd = 0;
                        model.PayFeeRatio = 0;
                        model.CrossBankPay = 3;
                        model.DepositType = "";
                        break;
                    case 2://中转卡
                        model.PaymentStart = 0;
                        model.PaymentEnd = 0;
                        model.PayFeeRatio = 0;
                        model.CrossBankPay = 3;
                        model.DepositType = "";
                        break;
                    case 3://付款卡
                        model.DepositFeeRatio = 0;
                        break;
                    case 4://备用卡
                        model.PaymentStart = 0;
                        model.PaymentEnd = 0;
                        model.PayFeeRatio = 0;
                        model.DepositFeeRatio = 0;
                        model.CrossBankPay = 3;
                        model.DepositType = "";
                        break;
                    default:
                        break;
                }
                var resultDao = _bankCardDao.AddBankCard(model, isSpare);
                if (resultDao)
                {
                    //日志
                    _logRecord.LogType = LogRecordLogType.BankCard_Add.GetHashCode();
                    _logRecord.LogLevel = (sbyte)LogLevel.Success.GetHashCode();
                    _logRecord.CreateDate = DateTime.Now;
                    _logRecord.LogRemark = string.Format("新增銀行卡成功,银行卡号：{0}", model.CardNumber);
                    _logDao.Insert(_logRecord);
                    result.Success = true;
                }
                else
                {
                    throw new Exception("新增銀行卡信息失败！");
                }
            }
            catch (Exception ex)
            {
                try
                {
                    //日志
                    _logRecord.LogType = LogRecordLogType.BankCard_Add.GetHashCode();
                    _logRecord.LogLevel = (sbyte)LogLevel.Error.GetHashCode();
                    _logRecord.CreateDate = DateTime.Now;
                    _logRecord.LogRemark = string.Format("新增銀行卡失败,银行卡号：{0}", model.CardNumber);
                    //_logRecord.RequestData = request.RequestData;
                    _logDao.Insert(_logRecord);

                    result.ErrorMessage = ex.Message;
                }
                catch (Exception e)
                {
                    result.ErrorMessage = e.Message;
                }
            }
            return result;
        }

        /// <summary>
        /// 修改銀行卡信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public JResult UpdateBankCard(BankCard model, BankCardExtraLimit modelEx, Request request)
        {
            JResult result = new JResult()
            {
                Success = false
            };
            try
            {
                var resultDao = _bankCardDao.UpdateBankCard(model, modelEx);
                if (resultDao)
                {
                    //日志
                    _logRecord.LogType = LogRecordLogType.BankCard_Update.GetHashCode();
                    _logRecord.LogLevel = (sbyte)LogLevel.Success.GetHashCode();
                    _logRecord.CreateDate = DateTime.Now;
                    _logRecord.LogRemark = string.Format("修改銀行卡成功,银行卡号：{0}", model.CardNumber);
                    _logDao.Insert(_logRecord);
                    result.Success = true;
                }
                else
                {
                    throw new Exception("修改銀行卡信息失败！");
                }
            }
            catch (Exception ex)
            {
                try
                {
                    //日志
                    _logRecord.LogType = LogRecordLogType.BankCard_Update.GetHashCode();
                    _logRecord.LogLevel = (sbyte)LogLevel.Error.GetHashCode();
                    _logRecord.CreateDate = DateTime.Now;
                    _logRecord.LogRemark = string.Format("修改銀行卡失败,银行卡号：{0}", model.CardNumber);
                    _logRecord.RequestData = request.RequestData;
                    _logDao.Insert(_logRecord);

                    result.ErrorMessage = ex.Message;
                }
                catch (Exception e)
                {
                    result.ErrorMessage = e.Message;
                }
            }
            return result;
        }
        /// <summary>
        /// 刪除銀行卡
        /// </summary>
        /// <param name="bcid"></param>
        /// <returns></returns>
        public JResult DeleteBankCard(int Bcid, Request request, bool isSpare = false)
        {
            JResult result = new JResult()
            {
                Success = false
            };
            try
            {
                var bankCard = _bankCardDao.GetBankCardByBcid(Bcid);
                if (bankCard == null) {
                    result.ErrorMessage = "银行卡不存在";
                    return result;
                }

                var orderRecord = _menuDao.GetOrderRecordByOrderNo(bankCard.OrderNo);
                if (orderRecord == null)
                {
                    result.ErrorMessage = "订单记录不存在";
                    return result;
                }

                if (orderRecord.ReceiptUid > 0) {
                    result.ErrorMessage = "订单已确认收货，不能删除";
                    return result;
                }

                var resultDao = _bankCardDao.DeleteBankCard(Bcid, isSpare);
                if (resultDao)
                {
                    if(isSpare)//库存卡
                    {
                        //日志
                        _logRecord.LogType = LogRecordLogType.BankCardPare_Delete.GetHashCode();
                        _logRecord.LogLevel = (sbyte)LogLevel.Success.GetHashCode();
                        _logRecord.CreateDate = DateTime.Now;
                        _logRecord.LogRemark = string.Format("删除库存銀行卡成功,银行卡ID：{0}", Bcid);
                        _logDao.Insert(_logRecord);
                        result.Success = true;
                    }
                    else
                    {
                        //日志
                        _logRecord.LogType = LogRecordLogType.BankCard_Delete.GetHashCode();
                        _logRecord.LogLevel = (sbyte)LogLevel.Success.GetHashCode();
                        _logRecord.CreateDate = DateTime.Now;
                        _logRecord.LogRemark = string.Format("删除銀行卡成功,银行卡ID：{0}", Bcid);
                        _logDao.Insert(_logRecord);
                        result.Success = true;
                    }
                }
                else
                {
                    throw new Exception("删除銀行卡失败！");
                }
            }
            catch (Exception ex)
            {
                try
                {
                    if(isSpare)
                    {
                        //日志
                        _logRecord.LogType = LogRecordLogType.BankCardPare_Delete.GetHashCode();
                        _logRecord.LogLevel = (sbyte)LogLevel.Error.GetHashCode();
                        _logRecord.CreateDate = DateTime.Now;
                        _logRecord.LogRemark = string.Format("删除库存銀行卡失败,银行卡ID：{0}", Bcid);
                        _logRecord.RequestData = request.RequestData;
                        _logDao.Insert(_logRecord);
                    }
                    else
                    {
                        //日志
                        _logRecord.LogType = LogRecordLogType.BankCard_Delete.GetHashCode();
                        _logRecord.LogLevel = (sbyte)LogLevel.Error.GetHashCode();
                        _logRecord.CreateDate = DateTime.Now;
                        _logRecord.LogRemark = string.Format("删除銀行卡失败,银行卡ID：{0}", Bcid);
                        _logRecord.RequestData = request.RequestData;
                        _logDao.Insert(_logRecord);
                    }
                    result.ErrorMessage = ex.Message;
                }
                catch (Exception e)
                {
                    result.ErrorMessage = e.Message;
                }
            }
            return result;

        }
        /// <summary>
        /// 更改银行卡启用状态
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public JResult ChangeBankCardSta(BankCard model, Request request)
        {
            JResult result = new JResult()
            {
                Success =false
            };
            try
            {
                var resultDao = _bankCardDao.ChangeBankCardSta(model);
                if (resultDao)
                {
                    //日志
                    _logRecord.LogType = LogRecordLogType.BankCard_State.GetHashCode();
                    _logRecord.LogLevel = (sbyte)LogLevel.Success.GetHashCode();
                    _logRecord.CreateDate = DateTime.Now;
                    _logRecord.LogRemark = string.Format("更改银行卡启用状态成功,银行卡启用状态：{0}", model.EnableStatus);
                    _logDao.Insert(_logRecord);
                    result.Success = true;
                }
                else
                {
                    throw new Exception("更改银行卡启用状态失败！");
                }
            }
            catch (Exception ex)
            {
                try
                {
                    //日志
                    _logRecord.LogType = LogRecordLogType.BankCard_State.GetHashCode();
                    _logRecord.LogLevel = (sbyte)LogLevel.Error.GetHashCode();
                    _logRecord.CreateDate = DateTime.Now;
                    _logRecord.LogRemark = string.Format("更改银行卡启用状态失败,银行卡启用状态：{0}", model.EnableStatus);
                    _logRecord.RequestData = request.RequestData;
                    _logDao.Insert(_logRecord);

                    result.ErrorMessage = ex.Message;
                }
                catch (Exception e)
                {
                    result.ErrorMessage = e.Message;
                }
            }
            return result;
        }


        /// <summary>
        /// 编辑虚拟机编码和备注
        /// </summary>
        /// <param name="request"></param>
        public JResult BankCardEditOther(BankCard request) {
            JResult result = new JResult()
            {
                Success = false
            };
            try
            {
                //日志
                _logRecord.LogType = LogRecordLogType.BankCard_Update.GetHashCode();
                _logRecord.LogLevel = (sbyte)LogLevel.Error.GetHashCode();
                _logRecord.CreateDate = DateTime.Now;
                _logRecord.RequestData = JsonConvert.SerializeObject(request);

                var bankCard = _bankCardDao.GetBankCardByBcid(request.Bcid);
                if (bankCard == null)
                {
                    result.ErrorMessage = "记录不存在,无法修改";
                    return result;
                }
                
                if (string.IsNullOrEmpty(request.VmClientId))
                {
                    bankCard.VmClientId = null;
                }
                else
                {
                    bankCard.VmClientId = request.VmClientId;
                }
                bankCard.VmClientIdUpdateDate = DateTime.Now;
                if (string.IsNullOrEmpty(request.Remark))
                {
                    bankCard.Remark = null;
                }
                else
                {
                    bankCard.Remark = request.Remark;
                }
                _logRecord.AffectData = JsonConvert.SerializeObject(bankCard);

                var isUpdate = _bankCardDao.Update(bankCard);
                if (isUpdate)
                {
                    result.Success = true;
                    result.SuccessMessage = "修改成功";
                    _logRecord.LogRemark = string.Format(result.SuccessMessage);
                }
                else
                {
                    result.ErrorMessage = "修改失败";
                    _logRecord.LogRemark = string.Format(result.ErrorMessage);
                }
                
            }
            catch (Exception)
            {
                _logRecord.LogRemark = string.Format("修改虚拟机编码和备注出错");
            }

            _logDao.Insert(_logRecord);

            return result;
        }

    }
}
